home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / eflibpt4.zip / DEMO / STREAMS / SEARCH.PAS < prev    next >
Pascal/Delphi Source File  |  1996-08-18  |  2KB  |  47 lines

  1. { Borland Pascal Extended Function Library - EFLIB (C) Johan Larsson, 1996
  2.   Demonstration; simple searching of stream contents
  3.  
  4.   EFLIB IS PROTECTED BY THE COPYRIGHT LAW AND MAY NOT BE COPIED, SOLD OR
  5.   MANIPULATED. FOR MORE INFORMATION, SEE PROGRAM MANUAL! THIS DEMONSTRAT-
  6.   ION PROGRAM MAY FREELY BE USED AND DISTRIBUTED.                          }
  7.  
  8.  
  9. uses EFLIBDEF, EFLIBIO, CRT;
  10.  
  11. const SearchCache = 16000; { Higher value gives better perfomance }
  12.  
  13. var SearchStream : StreamObjectPointerType; Data : string;
  14.  
  15.  
  16. begin
  17.      if FileExists(ParamStr(1)) and (ParamStr(2) <> '') then begin
  18.         WriteLn ('* Searching *');
  19.  
  20.         { Initialize a file stream that we will search }
  21.         SearchStream := New (FileStreamObjectPointerType,
  22.                              Initialize (ParamStr(1), SearchCache));
  23.  
  24.         { Perform an un-optimized search; 1) read string of
  25.           the same length as the searched string from the stream,
  26.           2) compare strings and 3) walk backwards to the character
  27.           after beginning of the read string. }
  28.  
  29.         while not SearchStream^.IsEnd and (Data <> ParamStr(2)) do
  30.               with SearchStream^ do begin
  31.  
  32.               { Read a string from the stream }
  33.               Read (Data[1], Length(ParamStr(2)));
  34.               Data[0] := Chr(LastTransfer);
  35.               { Seek backwards }
  36.               if (Position > Pred(Length(ParamStr(2)))) and (Position < Size) then
  37.                  Seek (Position - Pred(Length(ParamStr(2))));
  38.  
  39.         end;
  40.  
  41.         if (Data = ParamStr(2)) then WriteLn ('Found at ', SearchStream^.Position, '.')
  42.            else WriteLn ('Not found.');
  43.  
  44.         SearchStream^.Free;
  45.  
  46.      end else WriteLn ('This program only run with file and search string parameters.');
  47. end.